package ua.cn.stu.timer;

import java.util.Date;

//Class for program event timing
public class Timer {
	private Date start;
	
	//Reset timer
	public void reset(){
		start = new Date();
	}
	
	//The constructor, which starts a timer
	public Timer (){
		reset();
	}
	
	//Returns exact number of milliseconds since timer was started
	public long getTime(){
		Date now = new Date();
		long ms = now.getTime() - start.getTime();
		
		return ms;
	}


	 // Returns a formatted string time
	  public String toString()
	  {
	    long ms = getTime();
	    
	    long hours   = ms / 1000 / 60 / 60;
	    ms -= hours * 1000 * 60 * 60;
	      
	    long min = ms / 1000 / 60;
	    ms -= min * 1000  * 60;

	    long sec = ms / 1000;
	    ms -= sec * 1000;
	    
	    StringBuffer time = new StringBuffer();
	    if (hours > 0) time.append (hours + ":");
	    if (hours > 0 && min < 10) time.append ("0");
	    time.append (min + ":");
	    if (sec < 10) time.append ("0");
	    time.append (sec);
	    time.append (".");
	    if (ms < 100) time.append ("0");
	    if (ms <  10) time.append ("0");
	    time.append (ms);
	    
	    return time.toString();
	  }
	  
}
